package com.wxm.jsoup.util;

import com.wxm.jsoup.exception.JSOUPException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.Arrays;
import java.util.List;


/**
 * <p>
 *      Jsoup工具类
 * </p>
 *
 * @author: wxm
 * @Date: 2021/9/28
 */
public class JsoupUtils {

    /**
     * 对url进行连接尝试，成功返回Document 失败抛出异常
     * @param url 连接URL
     * @return Document
     */
    public static Document connect(String url){
        try {
            return Jsoup.connect(url).timeout(3 * 1000).get();
        } catch (Exception e) {
            JSOUPException.error("连接URL："+ url +  "失败 [" +e.getMessage() + "]");
        }
        return null;
    }

    /**
     * 对url进行连接尝试，成功返回Document 失败抛出异常
     * @param url 连接URL
     * @param second 连接秒数
     * @return Document
     */
    public static Document connect(String url,Integer second){
        try {
            return Jsoup.connect(url).timeout(second * 1000).get();
        } catch (Exception e) {
            JSOUPException.error("连接URL："+ url +  "失败 [" +e.getMessage() + "]");
        }
        return null;
    }

    /**
     * 根据selector获取复合的Element
     * @param element element
     * @param selector css选择器表达式
     * @return Element
     */
    public static Element getElementObject(Element element,String selector){
        Elements select = element.select(selector);
        if (select.size() >  0)JSOUPException.error("缺失的表达式 ："+ selector);
        return select.get(0);
    }

    /**
     * 根据selector获取复合的Elements
     * @param element element
     * @param selector css选择器表达式
     * @return Element
     */
    public static List<Element> getElementCollection(Element element, String selector){
        Elements select = element.select(selector);
        if (select.size() >  0)JSOUPException.error("缺失的表达式 ："+ selector);
        return select;
    }


    /**
     * 对Element进行select解析
     * @param element Element
     * @param selectSplit select解析字符串
     * @param indexSplit 解析后对应保留下标
     * @param must 是否必须字段
     * @return Element
     */
    public static Element getElementIndex(Element element, String[] selectSplit,String[] indexSplit,Boolean must){
        Element e = element;
        for (int i = 0; i < selectSplit.length; i++) {
            Elements elements = e.select(selectSplit[i]);
            //必须返回
            if (must){
                if (elements.size() <= 0){
                    JSOUPException.error(e.toString() + "\r 未找到 " + selectSplit[i] + " 元素");
                }else {
                    e = elements.get(Integer.parseInt(indexSplit[i]));
                }
            }else {
                if (elements.size() <= 0){
                    e = new Element("span");
                }else {
                    e = elements.get(Integer.parseInt(indexSplit[i]));
                }
            }
        }
        return e;
    }



    /**
     * 对Element进行select解析
     * @param element Element
     * @param selectSplit select解析字符串
     * @param indexSplit 解析后对应保留下标
     * @return Elements
     */
    public static Elements getElementIndexs(Element element, String[] selectSplit,String[] indexSplit){
        Element e = element;
        Elements es = null;
        try {
            for (int i = 0; i < selectSplit.length; i++) {
                int index = Integer.parseInt(indexSplit[i]);
                if (index != -1){
                    Elements select = e.select(selectSplit[i]);
                    if (select.size() <= 0){
                        JSOUPException.error(e + "\r未解析到："+selectSplit[i] + "元素");
                    }
                    e = select.get(index);
                }else {
                    es = e.select(selectSplit[i]);
                }
            }
        }catch (IndexOutOfBoundsException ex){
            JSOUPException.error("select解析失败 缺失的index数据 ：" + element + "\r" + Arrays.asList(selectSplit) + " " + Arrays.asList(indexSplit));
        }
        return es;
    }
}
